/**
 * $Id: FreemarkerUtils.java,v 1.0 2020/7/15 0015 11:23 qiaoyong Exp $
 * <p>
 * Copyright 2018 zonekey Technologies(China),Inc. All rights reserved.
 */
package com.zonekey.freemarkertest.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.UUID;

/**
 * @author qiaoyong
 * @version $Id: FreemarkerUtils.java,v 1.1 2020/7/15 0015 11:23 qiaoyong Exp $
 */
public class FreemarkerUtils {
    public static String getTemplate(String template, Map<String,Object> map) throws IOException, TemplateException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        String templatePath = FreemarkerUtils.class.getResource("/").getPath()+"/templates";
        cfg.setDirectoryForTemplateLoading(new File(templatePath));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setLogTemplateExceptions(false);
        cfg.setWrapUncheckedExceptions(true);
        Template temp = cfg.getTemplate(template);
        //StringWriter stringWriter = new StringWriter();
        String path = "E:\\develop\\gitee.com\\filePath/"+ UUID.randomUUID()+".html";
        File file = new File(path);
        FileWriter fileWriter = new FileWriter(file);
        temp.process(map, fileWriter);
        fileWriter.flush();
        fileWriter.close();
        return path;
    }
}
